{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is intended to demonstrate how vessel segmentation methods of ITKTubeTK can be applied to multi-channel MRI (MRA + T1, T2, etc)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "from itkwidgets import view\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ImageType = itk.Image[itk.F, 3]\n",
    "dir = \"../Data/CTP-MinMax/\"\n",
    "im1iso = itk.imread(dir + \"diff3.mha\")\n",
    "im1BrainVess = itk.imread(dir + \"diff3-Brain-VesselEnhanced.mha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "imMath = ttk.ImageMath.New(im1BrainVess)\n",
    "imMath.MedianFilter(1)\n",
    "imMath.Threshold(0.000001, 1, 1, 0)\n",
    "im1VessMask = imMath.GetOutputShort()\n",
    "\n",
    "ccSeg = ttk.SegmentConnectedComponents.New(im1VessMask)\n",
    "ccSeg.SetMinimumVolume(10)\n",
    "ccSeg.Update()\n",
    "im1VessMaskCC = ccSeg.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c8ca5598d82749859909aa31a22420cd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageSS3; pr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im1VessMaskCC)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "imMathSS = ttk.ImageMath.New(im1VessMaskCC)\n",
    "imMathSS.Threshold(0,0,1,0)\n",
    "im1VessMaskInv = imMathSS.GetOutputFloat()\n",
    "\n",
    "distFilter = itk.DanielssonDistanceMapImageFilter.New(im1VessMaskInv)\n",
    "distFilter.Update()\n",
    "dist = distFilter.GetOutput()\n",
    "\n",
    "imMath.SetInput(dist)\n",
    "imMath.Blur(0.4)\n",
    "tmp = imMath.GetOutput()\n",
    "imMath.ReplaceValuesOutsideMaskRange(tmp, 0.1, 10, 0)\n",
    "im1SeedRadius = imMath.GetOutput()\n",
    "\n",
    "itk.imwrite(im1SeedRadius, dir+\"diff3-VesselsSeedRadius.mha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "18607326b3d14f3dab19ce042f2e7ca9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(im1SeedRadius)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "acd2c6ba7aa64166b5c4b0dbe7d8ffe3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "imMath.SetInput(im1iso)\n",
    "imMath.ReplaceValuesOutsideMaskRange(im1BrainVess, 0, 1000, 0)\n",
    "imMath.Blur(0.4)\n",
    "imMath.IntensityWindow(0.5,1000,0,1000)\n",
    "im1Input = imMath.GetOutput()\n",
    "\n",
    "itk.imwrite(im1iso, dir+\"diff3-VesselsInput.mha\")\n",
    "\n",
    "view(im1Input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "numSeeds = 40\n",
    "\n",
    "vSeg = ttk.SegmentTubes.New(Input=im1Input)\n",
    "#vSeg.SetVerbose(True)\n",
    "vSeg.SetMinCurvature(0)#.0001)\n",
    "vSeg.SetMinRoundness(0.02)\n",
    "vSeg.SetMinRidgeness(0.5)\n",
    "vSeg.SetMinLevelness(0.0)\n",
    "vSeg.SetRadiusInObjectSpace( 0.8 )\n",
    "vSeg.SetBorderInIndexSpace(3)\n",
    "vSeg.SetSeedMask( im1SeedRadius )\n",
    "#vSeg.SetSeedRadiusMask( im1SeedRadius )\n",
    "vSeg.SetOptimizeRadius(True)\n",
    "vSeg.SetUseSeedMaskAsProbabilities(True)\n",
    "vSeg.SetSeedExtractionMinimumProbability(0.4)\n",
    "#vSeg.SetSeedMaskMaximumNumberOfPoints( numSeeds )\n",
    "vSeg.ProcessSeeds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f5266bea3f1244a0a992831ffdb63189",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tubeMaskImage = vSeg.GetTubeMaskImage()\n",
    "view(tubeMaskImage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "SOWriter = itk.SpatialObjectWriter[3].New()\n",
    "SOWriter.SetInput(vSeg.GetTubeGroup())\n",
    "SOWriter.SetBinaryPoints(True)\n",
    "SOWriter.SetFileName( dir+\"diff3-Vessels.tre\" )\n",
    "SOWriter.Update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "VTPWriter = itk.WriteTubesAsPolyData.New()\n",
    "VTPWriter.SetInput(vSeg.GetTubeGroup())\n",
    "VTPWriter.SetFileName(dir+\"diff3-Vessels.vtp\")\n",
    "VTPWriter.Update()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
